﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonalAlbum.Data.Objects;

namespace PersonalAlbum.Data.Entities
{
    public class SectionRepository : ISectionRepository
    {
        private string _connectionString;
        public SectionRepository(string connectionString)
        {
            this._connectionString = connectionString;
        }
        #region IRepository<Section> Members

        public List<PersonalAlbum.Data.Objects.Section> GetItems()
        {
            return GetItems(true);
        }
        public List<PersonalAlbum.Data.Objects.Section> GetItems(bool visible)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.Section.Where(s => (visible ? s.Visible : true)).Select(s =>
                    new PersonalAlbum.Data.Objects.Section()
                    {
                        ContentUrl = s.ContentUrl,
                        Description = s.Description,
                        DisplayOrder = s.DisplayOrder,
                        Id = s.Id,
                        Name = s.Name,
                        Visible = s.Visible
                    }).ToList();
            }
        }

        public void AddItem(PersonalAlbum.Data.Objects.Section item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Section s = new ImagesDal.Section()
                {
                    Visible = item.Visible,
                    Name = item.Name,
                    DisplayOrder = item.DisplayOrder,
                    Description = item.Description,
                    ContentUrl = item.ContentUrl
                };
                ImagesDB.AddToSection(s);
                ImagesDB.SaveChanges();
                item.Id = s.Id;
            }
        }

        public void DeleteItem(PersonalAlbum.Data.Objects.Section item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var old = ImagesDB.Section.FirstOrDefault(s => s.Id == item.Id);
                if (old != null)
                {
                    ImagesDB.DeleteObject(old);
                    ImagesDB.SaveChanges();
                }
            }
        }

        public void UpdateItem(PersonalAlbum.Data.Objects.Section item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var old = ImagesDB.Section.FirstOrDefault(s => s.Id == item.Id);
                if (old != null)
                {
                    old.ContentUrl = item.ContentUrl;
                    old.Description = item.Description;
                    old.DisplayOrder = item.DisplayOrder;
                    old.Name = item.Name;
                    old.Visible = item.Visible;
                    ImagesDB.SaveChanges();
                }
            }
        }

        #endregion

        #region ISectionRepository Members

        public PersonalAlbum.Data.Objects.Section GetItemById(int sectionId)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var item = ImagesDB.Section.FirstOrDefault(s => s.Id == sectionId);
                PersonalAlbum.Data.Objects.Section section = null;
                if (item != null)
                {
                    section = new PersonalAlbum.Data.Objects.Section()
                    {
                        Id = item.Id,
                        ContentUrl = item.ContentUrl,
                        Description = item.Description,
                        DisplayOrder = item.DisplayOrder,
                        Name = item.Name,
                        Visible = item.Visible
                    };
                }
                return section;
            }
        }

        #endregion
    }
}
